<?php

namespace app\api\controller\user;

use app\api\controller\Controller;
use app\api\model\plus\agent\Setting;
use app\api\model\user\User as UserModel;
use app\api\model\order\Order as OrderModel;
use app\api\model\order\OrderReferee as OrderRefereeModel;
use app\api\model\settings\Setting as SettingModel;
use app\api\model\plus\coupon\UserCoupon as UserCouponModel;
use app\common\enum\settings\GetPhoneTypeEnum;
use think\facade\Cache;
use app\api\model\supplier\Supplier as SupplierModel;
use app\api\model\plus\chat\Chat as ChatModel;

/**
 * 个人中心主页
 */
class Index extends Controller
{
    /**
     * 获取当前用户信息
     */
    public function detail($source = 'wx')
    {
        // 当前用户信息
        $user = $this->getUser();
        //店铺信息
        $user['is_recycle'] = $user['supplierUser'] ? SupplierModel::detail($user['supplierUser']['shop_supplier_id'])['is_recycle'] : '';
        $coupon_model = new UserCouponModel();
        $coupon = count($coupon_model->getList($user['user_id'], -1, false, false));
        // 订单总数
        $model = new OrderModel;

        // 分销商基本设置
        $setting = Setting::getItem('basic');
        // 是否开启分销功能
        $agent_open = $setting['is_open'];
        //商城设置
        $store = SettingModel::getItem('store');
        //供应商入住背景图
        $supplier_image = isset($store['supplier_image']) ? $store['supplier_image'] : '';
        // 充值功能是否开启
        $balance_setting = SettingModel::getItem('balance');
        $balance_open = intval($balance_setting['is_open']);
        //首单数据
        $firstOrder = $model->getCount($user, 'all',true);
        $firstYN = ($firstOrder>0)?0:1;
        return $this->renderSuccess('', [
            'coupon' => $coupon,
            'userInfo' => $user,
            'newPeople' => $firstYN,
            'orderCount' => [
                'payment' => $model->getCount($user, 'payment'),
                'delivery' => $model->getCount($user, 'delivery'),
                'received' => $model->getCount($user, 'received'),
                'comment' => $model->getCount($user, 'comment'),
            ],
            'setting' => [
                'points_name' => SettingModel::getPointsName(),
                'agent_open' => $agent_open,
                'supplier_image' => $supplier_image,
                'balance_open' => $balance_open
            ],
            'sign' => SettingModel::getItem('sign'),
            'getPhone' => $this->isGetPhone(),
            'msgcount' => (new ChatModel)->mCount($user),
            'menus' => UserModel::getMenus($user, $source),   // 个人中心菜单列表
            'supplierStatus' => SupplierModel::getStatus($user),
        ]);
    }

    /**
     * 当前用户设置
     */
    public function setting()
    {
        // 当前用户信息
        $user = $this->getUser();

        return $this->renderSuccess('', [
            'userInfo' => $user
        ]);
    }

    private function isGetPhone()
    {
        $user = $this->getUser();
        if ($user['mobile'] != '') {
            return false;
        }
        $settings = SettingModel::getItem('getPhone');
        if (in_array(GetPhoneTypeEnum::USER, $settings['area_type'])) {
            // 缓存时间
            $key = 'get_phone_' . $user['user_id'];
            if (!$data = Cache::get($key)) {
                $settings['send_day'] > 0 && Cache::set($key, '1', 86400 * $settings['send_day']);
                return true;
            }
        }
        return false;
    }

    /**
     * @Exp    获取邀请数据
     * @Author Mr.Z
     * @Date   2024-02-21
     * @return [type]     [description]
     */
    public function getdayinvite()
    {
        // 当前用户信息
        $user = $this->getUser();
        $OrderRefereeModel = new OrderRefereeModel();
        $UserModel = new UserModel();

        $param = [];
        $param['start_time'] = strtotime(date("Y-m-1",time()));
        $param['end_time'] = time();

        $params = [];
        $params['start_time'] = time();


        // 本月的起始日期，0点0分0秒
        $first_day_month = strtotime(date('Y-m-01 00:00:00'));
        // 本月的结束日期，下个月的第一天 - 1秒
        $last_day_Month = strtotime(date('Y-m-t 23:59:59'));
        $map = [];
        $map['start_time'] = $first_day_month;
//        $map['end_time'] = $last_day_Month;

        $list = [];
        $list['sum_result'] = [
            'month_price'=>$OrderRefereeModel->getSumResult($user['user_id'],$param,'pay_price'),
            'month_num'=>$OrderRefereeModel->getSumResult($user['user_id'],$param),
            'day_num'=>$UserModel->getReUserSum($user['user_id'],$params),
            'user_month_num'=>$UserModel->getReUserSum($user['user_id'],$map)
        ];
        
        return $this->renderSuccess('', $list);
    }
}